<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\JuheH5\AccountService;
use App\Services\PhoneCodeService;
use Illuminate\Http\Request;
use YQ\YqValidator;

class PhoneCodeController extends Controller
{
    /**
     * 获取手机验证码
     */
    public function get(Request $request)
    {
        $phone = $request->input('phone');
        $type  = $request->input('type', 0);
        if (YqValidator::phone($phone) === false) {
            return yqerr(7004);
        }

        if (!$type) {
            return yqerr(7017);
        }

        if(in_array($type, [PhoneCodeService::PHONE_CODE_TYPE_LOGIN, PhoneCodeService::PHONE_CODE_TYPE_RETRIEVE])) {
            $res = AccountService::getInstance()->isPhoneExisted($phone);
            if ($res !== true) {
                return yqerr(7013);
            }
        }

        $res = PhoneCodeService::getInstance()->send($type, $phone);
        if ($res !== true) {
            return yqerr($res);
        }

        return yqok();
    }
}
